<template>
  <div class="synchronous">
    <div class="content">
      <div class="plantime">
        <div class="left">
          <el-form label-width="auto">
            <el-row>
              <el-col>
                <el-form-item label=" 菜单名称：">
                  <el-input
                    placeholder="请输入"
                    size="small"
                    style="width: 250px"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div class="right">
          <el-button
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >
            查询
          </el-button>
        </div>
      </div>

      <!-- ---- -->
      <div class="contentT">
        <div class="tips">
          <el-button type="primary" @click="openDialog('一级菜单')"
            >新建一级菜单</el-button
          >
          <el-button type="primary" @click="openDialog('二级菜单')"
            >新建二级菜单</el-button
          >
          <el-button type="primary" @click="openDialog('三级菜单')"
            >新建三级菜单</el-button
          >
          <DialogCom
            :dialogType="dialogType"
            :dialogVisible="dialogVisible"
            :dialogTitle="dialogTitle"
            :formData="formData"
            @open-second-level-dialog="openDialog('二级菜单')"
            @open-third-level-dialog="openDialog('三级菜单')"
            @return-to-one-level-menu="returnToOneLevelMenu"
            @return-to-previous-level="returnToPreviousLevel"
            @clear-input="clearInput"
            
          />
        </div>
        <el-table
          height="750"
          :key="tableKey"
          v-loading="listLoading"
          :data="list"
          :header-cell-style="{
            background: '#EAF1FF',
            'text-align': 'center',
            color: '#000',
          }"
          :cell-style="{ 'text-align': 'center' }"
          fit
          highlight-current-row
          @sort-change="sortChange"
          style="width: 100%"
          :border="false"
        >
          <el-table-column
            label="序号"
            prop="id"
            sortable="custom"
            align="center"
            width="80"
          >
            <template slot-scope="{ row }">
              <span>{{ row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column label="一级菜单" width="180px" align="center">
            <template slot-scope="{ row }">
              <span>{{
                row.timestamp | parseTime("{y}-{m}-{d} {h}:{i}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="是否启用" width="160px">
            <template slot-scope="{ row }">
              <span>{{ row.author }}</span>
            </template>
          </el-table-column>
          <el-table-column label="二级菜单" width="180px" align="center">
            <template slot-scope="{ row }">
              <span class="link-type" @click="handleUpdate(row)"></span>
              <el-tag>{{ row.type | typeFilter }}</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="是否启用" width="160px" align="center">
            <template slot-scope="{ row }">
              <span style="color: red">{{ row.reviewer }}</span>
            </template>
          </el-table-column>
          <el-table-column label="三级菜单" width="180px">
            <template slot-scope="{ row }">
              <svg-icon
                v-for="n in +row.importance"
                :key="n"
                icon-class="star"
                class="meta-item__icon"
              />
            </template>
          </el-table-column>
          <el-table-column label="是否启用" align="center" width="160px">
            <template slot-scope="{ row }">
              <span></span>
            </template>
          </el-table-column>
          <el-table-column
            label="URL参数"
            class-name="status-col"
            min-width="350px"
          >
            <template slot-scope="{ row }">
              <span>{{ row.title }}</span>
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            align="center"
            width="180px"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                @click="handleClick(scope.row)"
                type="text"
                size="small"
                >修改</el-button
              >
              <el-button type="text" size="small" @click="handlerdelete()"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="listQuery.page"
          :limit.sync="listQuery.limit"
          @pagination="getList"
        />
      </div>
    </div>
    <transition name="fade-transform" mode="out-in"> </transition>
  </div>
</template>

<script>
import { fetchList, fetchPv } from "@/api/article";
import waves from "@/directive/waves"; // waves directive
import { parseTime } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import DialogCom from "./component/DialogCom.vue";

const calendarTypeOptions = [
  { key: "CN", display_name: "China" },
  { key: "US", display_name: "USA" },
  { key: "JP", display_name: "Japan" },
  { key: "EU", display_name: "Eurozone" },
];
// arr to obj, such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name;

  return acc;
}, {});

export default {
  name: "ComplexTable",
  components: { Pagination, DialogCom },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger",
      };
      return statusMap[status];
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type];
    },
  },
  data() {
    return {
      dialogVisible: false,
      dialogType: "一级菜单", // 设置初始的对话框类型为一级菜单
      formData: {
        level1MenuCode: "", // 一级菜单编号
        level1MenuName: "", // 一级菜单名称
        level2MenuCode: "", // 二级菜单编号
        level2MenuName: "", // 二级菜单名称
        level3MenuCode: "", // 三级菜单编号
        level3MenuName: "", // 三级菜单名称
      },
      radio: "1",
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,

      listQuery: {
        page: 1,
        limit: 20,

        importance: undefined,
        title: undefined,
        type: undefined,
        sort: "+id",
      },

      temp: {
        id: undefined,
        importance: 1,
        remark: "",
        timestamp: new Date(),
        title: "",
        type: "",
        status: "published",
      },

      downloadLoading: false,
      modalFormLevel: "",
    };
  },
  created() {
    this.getList();
    // console.log(this.temp);
  },
  computed:{
       dialogTitle() {
      if (this.dialogType === '一级菜单') {
        return `新增${this.dialogType}`;
      } else if (this.dialogType === '二级菜单') {
        return `新增${this.dialogType}`;
      } else if (this.dialogType === '三级菜单') {
        return `新增${this.dialogType}`;
      }
      return '';
    },
  },
  mounted() {},
  methods: {
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then((response) => {
        // console.log(response);
        this.list = response.data.items;
        this.total = response.data.total;

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false;
        }, 1.5 * 1000);
      });
    },

    // 搜索
    handleFilter() {


      this.listQuery.page = 1;
      this.getList();
    },
     openDialog(type) {
          // 关闭对话框，确保每次打开时都是新的

      this.dialogVisible = false;
      this.dialogType = type;
      // 清空表单数据
      this.formData.level1MenuCode = '';
      this.formData.level1MenuName = '';
      this.formData.level2MenuCode = '';
      this.formData.level2MenuName = '';
      this.formData.level3MenuCode = '';
      this.formData.level3MenuName = '';

      this.dialogVisible = true;
    },
    returnToOneLevelMenu() {
      // 点击删除图标后返回一级菜单对话框的内容
      this.dialogType = '一级菜单';
    },
    returnToPreviousLevel() {
      // 点击删除图标后返回上一级菜单对话框的内容
      if (this.dialogType === '三级菜单') {
        this.dialogType = '二级菜单';
        this.formData.level3MenuCode = '';
        this.formData.level3MenuName = '';
      } else if (this.dialogType === '二级菜单') {
        this.dialogType = '一级菜单';
        this.formData.level2MenuCode = '';
        this.formData.level2MenuName = '';
      }
    },
    clearInput() {
      // 在关闭对话框时清空输入框的值
      this.dialogVisible=false
      // console.log( this.dialogVisible);
      this.formData.level1MenuName = '';
      this.formData.level2MenuName = '';
      this.formData.level3MenuName = '';

    },
  
    // handleModifyStatus(row, status) {
    //   this.$message({
    //     message: "操作成功",
    //     type: "success",
    //   });
    //   row.status = status;
    // },
    sortChange(data) {
      const { prop, order } = data;
      if (prop === "id") {
        this.sortByID(order);
      }
    },
    sortByID(order) {
      if (order === "ascending") {
        this.listQuery.sort = "+id";
      } else {
        this.listQuery.sort = "-id";
      }
      this.handleFilter();
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: "",
        timestamp: new Date(),
        title: "",
        status: "published",
        type: "",
      };
    },
    // 弹1

    handleClick(row) {
      console.log(row);
      
      switch (row.importance) {
        case 1:
          this.dialogType = '一级菜单';
          break;
        case 2:
          this.dialogType = '二级菜单';
          break;
        case 3:
          this.dialogType = '三级菜单';
          break;
        default:
          this.dialogType = '';
          break;
    }
       this.dialogVisible = true; // 打开对话框
    },
    handlerdelete() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    handleFetchPv(pv) {
      fetchPv(pv).then((response) => {
        this.pvData = response.data.pvData;
        this.dialogPvVisible = true;
      });
    },

    getSortClass: function (key) {
      const sort = this.listQuery.sort;
      return sort === `+${key}` ? "ascending" : "descending";
    },
  },
};
</script>

<style lang='scss' scoped>
@import "@/styles/Fadeinout.scss";
.synchronous {
  padding: 20px;
  .plantime {
    background-color: #fff;
    width: 100%;
    height: 64px;
    border-radius: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    text-align: center;
    padding: 20px;
    font-size: 14px;
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
    margin-bottom: 20px;
    .left {
      margin-top: 22px;
    }
  }
  .contentT {
    box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    background-color: #fff;
    height: 100%;
    width: 100%;
    padding: 20px;

    .tips {
      width: 100%;
      height: 22px;
      font-size: 16px;
      // margin-left: 20px;
      font-weight: 500;
      color: #5084fb;
      line-height: 22px;
      margin-bottom: 20px;
    
    }
  }
  .dialog-footer {
    text-align: center;
  }
}
</style>